//
//  MyReleaseListViewController.m
//  Commerce
//
//  Created by 高飞 on 15/4/28.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "MyReleaseListViewController.h"
#import "OwnerCell.h"
#import "HomeCell.h"
#import "OwnerdetailViewController.h"
#import "HomeDetailViewController.h"

@interface MyReleaseListViewController (){
    NSMutableArray *_dataArray;
}

@end

@implementation MyReleaseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:@"refreshDataGoodsList" object:nil];
    [self refreshData];
}

#pragma mark -请求数据
- (void)getDataList
{
    if (_isOwner) {
        [self getOwnerList];
    }else{
        [self getShipperList];
    }
}

- (void)getShipperList{//货源列表
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSString *uid = [[GlobalData sharedGlobalData] userinfoDic][@"UserID"];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"userid":uid,
                                           @"page_index":[NSString stringWithFormat:@"%d",self.page],
                                           @"page_size":[NSString stringWithFormat:@"%d",self.pageLimit],
                                           }];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@pro/mylist",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  [SVProgressHUD dismiss];
                                  NSLog(@"responseObject=%@",responseObject);
                                  if ([responseObject isKindOfClass:[NSArray class]]) {
                                      if ([(NSArray *)responseObject count] < self.pageLimit) {
                                          self.canLoadMore = NO;
                                      }else{
                                          self.canLoadMore = YES;
                                      }
                                      if (self.page == 1) {
                                          [self.tableView setContentOffset:CGPointZero animated:NO];
                                          [_dataArray removeAllObjects];
                                          [_dataArray addObjectsFromArray:responseObject];
                                          [self addItemsOnTop];
                                      }else{
                                          [_dataArray addObjectsFromArray:responseObject];
                                          [self addItemsOnBottom];
                                      }
                                  }else{
                                      [self loadMoreCompleted];
                                  }
                                  
                              }];
}

- (void)getOwnerList{//车源列表
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    NSString *uid = [[GlobalData sharedGlobalData] userinfoDic][@"UserID"];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{@"userid":uid,
                                           @"page_index":[NSString stringWithFormat:@"%d",self.page],
                                           @"page_size":[NSString stringWithFormat:@"%d",self.pageLimit],
                                           }];
    [[BaseNetwork alloc] initWithMethodUrl:[NSString stringWithFormat:@"%@car/mylist",URL_ROOT]
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  [SVProgressHUD dismiss];
                                  NSLog(@"responseObject=%@",responseObject);
                                  if ([responseObject isKindOfClass:[NSArray class]]) {
                                      if ([(NSArray *)responseObject count] < self.pageLimit) {
                                          self.canLoadMore = NO;
                                      }else{
                                          self.canLoadMore = YES;
                                      }
                                      if (self.page == 1) {
                                          [self.tableView setContentOffset:CGPointZero animated:NO];
                                          [_dataArray removeAllObjects];
                                          [_dataArray addObjectsFromArray:responseObject];
                                          [self addItemsOnTop];
                                      }else{
                                          [_dataArray addObjectsFromArray:responseObject];
                                          [self addItemsOnBottom];
                                      }
                                  }else{
                                      [self loadMoreCompleted];
                                  }
                                  
                              }];
}

#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isOwner) {
        static NSString *CellIdentifier = @"OwnerCell";
        
        OwnerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"OwnerCell" owner:self options:nil];
            cell = (OwnerCell *)[array objectAtIndex:0];
        }
        [cell configUIWithData:_dataArray[indexPath.row]];
        
        return cell;
    }else{
        static NSString *CellIdentifier = @"Cell";
        
        HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];
            cell = (HomeCell *)[array objectAtIndex:0];
        }
        [cell configUIWithData:_dataArray[indexPath.row]];
        
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_isOwner) {
        OwnerdetailViewController *vc = [[OwnerdetailViewController alloc] initWithNibName:nil bundle:nil];
        vc.isHiddenBook = YES;
        vc.carId =_dataArray[indexPath.row][@"CarID"];
        vc.navStr = _dataArray[indexPath.row][@"CarTypeName"];
        vc.goodsDic = _dataArray[indexPath.row];
        vc.isModify = YES;
        [(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];
    }else{
        HomeDetailViewController *vc = [[HomeDetailViewController alloc] initWithNibName:nil bundle:nil];
        vc.isHiddenBook = YES;
        vc.goodsId =_dataArray[indexPath.row][@"GoodsID"];
        vc.navStr = _dataArray[indexPath.row][@"GoodsName"];
        vc.goodsDic = _dataArray[indexPath.row];
        vc.isModify = YES;
        [(UINavigationController *)self.view.window.rootViewController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
